package com.study.profile;


import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringValueResolver;

import javax.sql.DataSource;

@Configuration
@PropertySource(value ={"classpath:/DataSource.Properties","classpath:/Application.properties"})
//@ComponentScan(value = {"com.study.profile"})
public class MainConfigProfile implements EmbeddedValueResolverAware{
    private StringValueResolver embeddedValueResolver;

    @Value("123456")
    private String passWord;

    @Profile("test")
    @Bean("dataSourceTest")
    public DataSource dataSourceTest(@Value("${jdbc.url}") String url) throws Exception{
        ComboPooledDataSource dataSource = new ComboPooledDataSource();

        dataSource.setPassword(passWord);
        dataSource.setUser("root");
        dataSource.setJdbcUrl(url);
        String s = this.embeddedValueResolver.resolveStringValue("${jdbc.driver}");
        dataSource.setDriverClass(s);
        return dataSource;
    }
    @Profile("dev")
    @Bean("dataSourceDev")
    public DataSource dataSourcesDev(@Value("${jdbc.url}") String url) throws Exception{
        ComboPooledDataSource dataSource = new ComboPooledDataSource();

        dataSource.setPassword(passWord);
        dataSource.setUser("root");
        dataSource.setJdbcUrl(url);
        String s = this.embeddedValueResolver.resolveStringValue("${jdbc.driver}");
        dataSource.setDriverClass(s);
        return dataSource;
    }




    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        this.embeddedValueResolver = resolver;
    }

    public String resolver(String s){

        return this.embeddedValueResolver.resolveStringValue(s);
    }
}
